package cn.lf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import cn.lf.constant.DelFlagTypeEnum;
import cn.lf.dto.product.ProductReqDTO;
import cn.lf.dto.product.ProductRespDTO;
import cn.lf.entity.Product;
import cn.lf.entity.ProductType;
import cn.lf.entity.ShopInfo;
import cn.lf.mapper.ProductMapper;
import cn.lf.mapper.ProductTypeMapper;
import cn.lf.mapper.ShopInfoMapper;
import cn.lf.service.IProductService;
import cn.lf.utils.SnowFlakeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品信息表(Product)表服务实现类
 *
 * @author makejava
 * @since 2024-04-03 00:00:42
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private SnowFlakeUtil snowFlakeUtil;

    @Resource
    private ProductTypeMapper productTypeMapper;

    @Resource
    private ShopInfoMapper shopInfoMapper;

    /**
     * 数据状态--停用
     */
    public static final String DATA_STATUS_DISABLED = "-1";


    /**
     * 数据状态--启用
     */
    public static final String DATA_STATUS_AVAILABLE = "0";

    /**
     * 通过ID查询单条数据
     *
     * @param pid 主键
     * @return 实例对象
     */
    @Override
    public ProductRespDTO queryById(String pid) {
        ProductRespDTO respDTO = new ProductRespDTO();
        Product product = this.productMapper.queryById(pid);
        //属性复制
        BeanUtil.copyProperties(product, respDTO);
        return respDTO;
    }

    /**
     * 分页查询
     *
     * @param reqDTO 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<ProductRespDTO> queryByPage(ProductReqDTO reqDTO) {

        LambdaQueryWrapper<ProductType> ptWrapper = new LambdaQueryWrapper<>();

        ptWrapper.eq(ProductType::getDelFlag, DelFlagTypeEnum.UN_DEL.getCode());

        List<ProductType> ptList = productTypeMapper.selectList(ptWrapper);

        List<ShopInfo> shopList = shopInfoMapper.selectAll();

        Product product = new Product();
        //属性复制
        BeanUtil.copyProperties(reqDTO, product);
        //开启分页
        PageHelper.startPage(reqDTO.getPageNum(), reqDTO.getPageSize());
        //商品列表
        List<Product> products = productMapper.queryAll(product);
        PageInfo pageInfo = new PageInfo(products);
        //结果列表
        List<ProductRespDTO> resultList = new ArrayList<>();
        products.stream().forEach(item -> {
            ProductRespDTO respDTO = new ProductRespDTO();
            BeanUtil.copyProperties(item, respDTO);
            List<ProductType> tempPtList = ptList.stream().filter(v -> Objects.equals(v.getProductTypeId(),item.getProductTypeId())).collect(Collectors.toList());
            if (!tempPtList.isEmpty()){
                respDTO.setProductTypeStr(tempPtList.get(0).getProductTypeName());
            }
            List<ShopInfo> tempShopList = shopList.stream().filter(v -> Objects.equals(v.getShopId(),item.getShopId())).collect(Collectors.toList());
            if (!tempShopList.isEmpty()){
                respDTO.setShopStr(tempShopList.get(0).getShopName());
            }
            resultList.add(respDTO);
        });
        pageInfo.setList(resultList);
        return pageInfo;
    }

    /**
     * 新增或更新数据
     *
     * @param reqDTO 实例对象
     * @return 实例对象
     */
    @Override
    public Integer addOrUpdate(ProductReqDTO reqDTO) {
        Integer flag;
        Product product = new Product();
        //新增
        if (ObjectUtil.isEmpty(reqDTO.getPid())) {
            BeanUtil.copyProperties(reqDTO, product);
            product.setPid(String.valueOf(new SnowFlakeUtil().generatorId()));
            flag = this.productMapper.insert(product);
        } else {
            //更新
            BeanUtil.copyProperties(reqDTO, product);
            flag = this.productMapper.update(product);
        }
        return flag;
    }


    /**
     * 通过主键删除数据
     *
     * @param pid 主键
     * @return 是否成功
     */
    @Override
    public Integer deleteById(String pid) {
        Product product = new Product();
        product.setPid(pid);
        product.setStatus(DATA_STATUS_DISABLED);
        return this.productMapper.update(product);
    }
}
